package com.qf.ssm.service.Impl;

import com.qf.ssm.entity.Brand;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.TBrandMapper;
import com.qf.ssm.service.SelectListService;
import com.qf.ssm.utils.ResponseCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SelectListServiceImpl implements SelectListService {
    @Resource
    TBrandMapper brandMapper;

    @Override
    public List<Brand> selectList() {
        try {
            return brandMapper.listBrand();
        } catch (Exception ex) {
            throw new ServiceException(ResponseCode.GROUP_SELECT);
        }
    }

    @Override
    public List<Brand> selectByIdList(int id) {
        try {
            return brandMapper.selectByIdList(id);
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.GROUP_SELECT);
        }

    }

    @Override
    public int updateById(int id) {

        try {

            return brandMapper.updateById(id);
        } catch (Exception ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }

    }

    @Override
    public int updateList(List<Integer> ids) {
        try {
            return brandMapper.updateList(ids);
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    @Override
    public Brand selectByName(String name) {
        try {
            Brand brand = brandMapper.selectByName(name);
            if(brand!=null){
                return brandMapper.selectByName(name);
            }else {
                throw new ServiceException(ResponseCode.BRAND_SELECT);
            }


        }catch (Exception e){
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    @Override
    public int insertNewBrand(Brand brand) {
        try {
            int i = brandMapper.insertNewBrand(brand);
            if (i>0){
                return i;
            }else {
                throw new ServiceException(ResponseCode.BRAND_INSERT);
            }

        }catch (Exception e){
            throw new ServiceException(ResponseCode.ERROR);
        }


    }
}
